Day 6: Probably a Fire Hazard

Because your neighbors keep defeating you in the holiday house decorating contest year after year, you've decided to deploy one million lights in a 1000x1000 grid.

Furthermore, because you've been especially nice this year, Santa has mailed you instructions on how to display the ideal lighting configuration.

Lights in your grid are numbered from 0 to 999 in each direction; the lights at each corner are at 0,0, 0,999, 999,999, and 999,0. The instructions include whether to turn on, turn off, or toggle various inclusive ranges given as coordinate pairs. Each coordinate pair represents opposite corners of a rectangle, inclusive; a coordinate pair like 0,0 through 2,2 therefore refers to 9 lights in a 3x3 square. The lights all start turned off.

To defeat your neighbors this year, all you have to do is set up your lights by doing the instructions Santa sent you in order.

For example:

turn on 0,0 through 999,999 would turn on (or leave on) every light.
toggle 0,0 through 999,0 would toggle the first line of 1000 lights, turning off the ones that were on, and turning on the ones that were off.
turn off 499,499 through 500,500 would turn off (or leave off) the middle four lights.
After following the instructions, how many lights are lit?

In [2]:
import re

In [42]:
_INST_EXP = re.compile("(turn on|toggle|turn off) ([0-9]*),([0-9]*) through ([0-9]*),([0-9]*)")
_ACTIONS_PART1 = { 'turn on': lambda x: True, 'turn off': lambda x: False, 'toggle': lambda x: not x}

def change_leds(leds, instruction, actions):
    action, fx, fy, tx, ty = _INST_EXP.match(instruction.rstrip()).groups()    
    action, fx, fy, tx, ty = actions[action], int(fx), int(fy), int(tx), int(ty)
    
    for x in range(fx, tx+1):
        for y in range(fy, ty+1):
            leds[x][y] = action(leds[x][y])
    
    return leds

def run_instructions_part1(instructions):
    leds = [[False for i in range(1000)] for i in range(1000)]
    
    for i in instructions:
        leds = change_leds(leds, i, _ACTIONS_PART1)
        
    # Count on leds
    return sum(sum(1 for i in l if i) for l in leds)

In [43]:
# Test example instructions
run_instructions_part1([
        "turn on 0,0 through 999,999", 
        "toggle 0,0 through 999,0", 
        "toggle 0,0 through 999,0", 
        "turn off 499,499 through 500,500"
    ])


Out[43]:
999996

In [44]:
with open('day6/input.txt', 'rt') as instructions:
    print("There are {} lit leds".format(run_instructions_part1(instructions)))


There are 569999 lit leds

Part Two

You just finish implementing your winning light pattern when you realize you mistranslated Santa's message from Ancient Nordic Elvish.

The light grid you bought actually has individual brightness controls; each light can have a brightness of zero or more. The lights all start at zero.

The phrase turn on actually means that you should increase the brightness of those lights by 1.

The phrase turn off actually means that you should decrease the brightness of those lights by 1, to a minimum of zero.

The phrase toggle actually means that you should increase the brightness of those lights by 2.

What is the total brightness of all lights combined after following Santa's instructions?

For example:

turn on 0,0 through 0,0 would increase the total brightness by 1.
toggle 0,0 through 999,999 would increase the total brightness by 2000000.

In [48]:
_ACTIONS_PART2 = { 'turn on': lambda x: x+1, 'turn off': lambda x: max(0,x-1), 'toggle': lambda x: x+2}

def run_instructions_part2(instructions):
    leds = [[0 for i in range(1000)] for i in range(1000)]
    
    for i in instructions:
        leds = change_leds(leds, i, _ACTIONS_PART2)
        
    # Count on leds
    return sum(sum(l) for l in leds)

In [49]:
# Test examples
run_instructions_part2([
        "turn on 0,0 through 999,999", 
        "toggle 0,0 through 999,0", 
        "toggle 0,0 through 999,0", 
        "turn off 499,499 through 500,500",
        "toggle 0,0 through 999,999", 
        "turn on 0,0 through 0,0"
        ])


Out[49]:
3003997

In [50]:
with open('day6/input.txt', 'rt') as instructions:
    print("Total brightness is {}".format(run_instructions_part2(instructions)))


Total brightness is 17836115

In [ ]: